from http import HTTPStatus
from typing import Any, Optional, List

from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel

from app.utils.json_utils import custom_encoder, json_encoders_deep
from app.utils.str_utils import str_prominent

MSG_OK = '操作成功'

MSG_ERR = "API调用失败"


# API返回的响应结果
class BaseResult(BaseModel):
    #提示信息
    msg: str = MSG_OK
    #状态码,200为成功,其它皆为失败
    code: int = HTTPStatus.OK
    # 返回的数据信息
    data: Optional[Any] = None

    def json_encoders(self):
        model_dict = dict(self.__dict__)
        if self.data:
            model_dict['data'] = json_encoders_deep(self.data)
            # if hasattr(self.data, 'json_encoders'):
            #     # print(str_prominent('data has json_encoders'))
            #     model_dict['data']=self.data.json_encoders()
        return jsonable_encoder(model_dict, custom_encoder=custom_encoder)


def okResult(data: Any = None, msg: str = MSG_OK):
    return BaseResult(code=HTTPStatus.OK, data=data, msg=msg)


def sksTableResult(list: Any = None, total:int=0):
    data={
        'list':list,
        'total':total
    }
    return BaseResult(code=HTTPStatus.OK, data=data, msg=MSG_OK)


def errResult(code:int=HTTPStatus.INTERNAL_SERVER_ERROR,data: Any = None, msg: str = MSG_ERR):
    return BaseResult(code=code, data=data, msg=msg)

#查询列表返回信息
class BaseListResult(BaseResult):
    rows: Optional[list] = None
    total: int = 0


#前端 sksTable 数据结构
class BaseSksTableResult(BaseResult):
    list: List[Any] = None
    total: int = 0
